package pld.car.action;

import java.io.File;
import java.util.*;
import com.opensymphony.xwork2.ActionContext;
import pld.car.bean.SettingsImg;
import pld.car.bean.Settings;
import pld.car.utils.Public;

public class ManageSettingsAction extends BaseAction {
	//页面初始化参数
	private String category;
	private String title;
	private String item;
	private String setKey;
	
	private List<Settings> hotlineList;
	private List<Settings> optionsList;
	private Settings settings;
	private List<Integer> imgDel;// 待删除img list
	private int imgID;
	private String[] imgTitle;
	private String[] imgNote;
	
	private Settings note;
	private String result;
	private File[] image;

	public ManageSettingsAction() {
		System.out.println("ManageSettingsAction init!");
	}

	/******************************************************************************************************************/
	/*************************************** Settings修改 ******************************************************************/
	/******************************************************************************************************************/

	public String loadSettingsPage() {
		ActionContext.getContext().getSession().put("category", this.getCategory());
		ActionContext.getContext().getSession().put("title", this.getTitle());
		ActionContext.getContext().getSession().put("item", this.getItem());
		ActionContext.getContext().getSession().put("setKey", this.getSetKey());
		return "success";
	}

	// 获取 根据传入的item和setKey查询对应的settings
	public String querySettings() {
		String item = (String) ActionContext.getContext().getSession().get("item");
		String setKey = (String) ActionContext.getContext().getSession().get("setKey");
		this.setSettings(this.getSettingsService().querySettings(item, setKey));
		this.imgDel = new ArrayList<Integer>();
		return "success";
	}

	//保存修改
	public String modifySettings() throws Exception {
		try {
			modifySettingsImg();
		} catch (Exception e) {
			System.out.println("保存失败。");
			e.printStackTrace();
		} finally {
			clearIt();
		}
		String item = (String) ActionContext.getContext().getSession().get("item");
		if (item.equals("Focus")){
			return "Focus";
		}else{
			return "success";
		}
	}

	//保存修改
	public String modifySettingsImg() throws Exception {
		// 生成新的imgSet，将需要删除的图片从set中剔除
		Set<SettingsImg> oldImages = this.getSettings().getSettingsImg();
		Set<SettingsImg> newImages = new LinkedHashSet<SettingsImg>();
		Iterator<SettingsImg> i = oldImages.iterator();
		while (i.hasNext()) {
			SettingsImg currentImg = (SettingsImg) i.next();
			int settingsImgID = currentImg.getSettingsImgID();
			boolean found = false;
			for (int j = 0; j < this.getImgDel().size(); j++) {
				int imgDel = this.getImgDel().get(j);
				if (imgDel == settingsImgID) {
					found = true;
					break;
				}
			}
			// 如果没有找到，那么这张图片没有删除，添加到newImages中
			if (!found) {
				newImages.add(currentImg);
			}
		}
		// 添加新的图片
		if (this.getImage() != null) {
			for (int j = 0; j < this.getImage().length; j++) {
				SettingsImg settingsImg = new SettingsImg();
				File imageFile = this.getImage()[j];
				settingsImg.setImg(Public.getByte(imageFile));
				settingsImg.setTitle(this.getImgTitle()[j]);
				settingsImg.setNote(this.getImgNote()[j]);
				newImages.add(settingsImg);
			}
		}
		this.settings.setSettingsImg(newImages);
		this.getSettingsService().update(this.settings);
		this.getSettingsService().deleteOrphanItems();
		return "success";
	}

	// 点击删除图片，将删除的图片ID添加到imageLeft列表
	public String delOneSettingsImg() {
		this.imgDel.add(this.getImgID());
		return "success";
	}


	/******************************************************************************************************************/
	/*************************************** 旅客心声 ******************************************************************/
	/******************************************************************************************************************/

	/* ==================================================================== */
	/* =========================服务热线=================================== */
	/* ==================================================================== */

	// 获取settings表中的关于热线的全部信息
	public String queryHotlineList() {
		this.setHotlineList(this.getSettingsService().queryHotlineList());
		return "success";
	}

	// 修改hotline的信息
	public String updateHotline() {
		this.getSettingsService().update(this.getHotlineList());
		this.setResult("热线信息修改成功！修改时间：" + Public.createDateTime());
		return "success";
	}

	/******************************************************************************************************************/
	/*************************************** 系统选项 ******************************************************************/
	/******************************************************************************************************************/
	// 获取 系统选项
	public String queryOptionsList() {
		this.setOptionsList(this.getSettingsService().queryOptions());
		return "success";
	}

	// 修改 系统选项
	public String updateOptionsList() {
		this.getSettingsService().update(this.getOptionsList());
		this.setResult("系统选项修改成功！修改时间：" + Public.createDateTime());
		return "success";
	}

	// 操作完成之后，将相关变量的值清空
	private void clearIt() {
		this.setImage(null);
	}

	public List<Settings> getHotlineList() {
		return hotlineList;
	}

	public void setHotlineList(List<Settings> hotlineList) {
		this.hotlineList = hotlineList;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public Settings getNote() {
		return note;
	}

	public void setNote(Settings note) {
		this.note = note;
	}

	public List<Settings> getOptionsList() {
		return optionsList;
	}

	public void setOptionsList(List<Settings> optionsList) {
		this.optionsList = optionsList;
	}

	public File[] getImage() {
		return image;
	}

	public void setImage(File[] image) {
		this.image = image;
	}

	public Settings getSettings() {
		return settings;
	}

	public void setSettings(Settings settings) {
		this.settings = settings;
	}

	public String getItem() {
		return item;
	}

	public void setItem(String item) {
		this.item = item;
	}

	public String getSetKey() {
		return setKey;
	}

	public void setSetKey(String setKey) {
		this.setKey = setKey;
	}

	public List<Integer> getImgDel() {
		return imgDel;
	}

	public void setImgDel(List<Integer> imgDel) {
		this.imgDel = imgDel;
	}

	public int getImgID() {
		return imgID;
	}

	public void setImgID(int imgID) {
		this.imgID = imgID;
	}

	public String[] getImgTitle() {
		return imgTitle;
	}

	public void setImgTitle(String[] imgTitle) {
		this.imgTitle = imgTitle;
	}

	public String[] getImgNote() {
		return imgNote;
	}

	public void setImgNote(String[] imgNote) {
		this.imgNote = imgNote;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) throws Exception{
		this.category = new String(category.getBytes("ISO-8859-1"),"UTF-8");
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) throws Exception{
		this.title = new String(title.getBytes("ISO-8859-1"),"UTF-8");
	}
}
